//
//  TabBarView.m
//  TravelApp
//  Created by 吴可高 on 15/10/10.
//  Copyright (c) 2015年 吴可高. All rights reserved.

#import "TabBarView.h"
#import "TabBarItemButton.h"

#define  KTabBarItem_Width  (kScreen_Width -Kpadding*12)/5
#define  Kpadding   5
#define  KTopMargin 2

@interface TabBarView()
{
    TabBarItemButton * _selectedTabBarItem;
}
@end

@implementation TabBarView

-(instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame])
    {
        NSArray *titleArray = @[@"首页",@"找产品",@"",@"我的订单",@"个人中心"];
        NSArray *imgArray = @[@"homePageUnSelected", @"classUnSelected", @"appMsgUnSelected", @"appOrderUnSelected", @"myCenterUnSelected"];
        NSArray *imgSelectedArray = @[@"homePageSelected", @"classSelected", @"appMsgSelected", @"appOrderSelected", @"myCenterSelected"];
        for (int index = 0 ;index < 5; index++)
        {
            TabBarItemButton*tabbarButton;
             tabbarButton= (TabBarItemButton*)[[TabBarItemButton alloc]init];
            [tabbarButton setTitle:[NSString stringWithFormat:@"%@",titleArray[index]] forState:UIControlStateNormal];
            [tabbarButton setImage:[UIImage imageNamed:imgArray[index]] forState:UIControlStateNormal];
            [tabbarButton setImage:[UIImage imageNamed:imgSelectedArray[index]] forState:UIControlStateSelected];
            
            tabbarButton.tag  = index + 1000;
            tabbarButton.titleLabel.font = [UIFont systemFontOfSize:13];
            tabbarButton.titleLabel.textAlignment = NSTextAlignmentCenter;
            [tabbarButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            [tabbarButton setTitleColor:UIColorFromRGB(0xf8c301) forState:UIControlStateSelected];
            [tabbarButton addTarget:self action:@selector(tabBarItemDidSelected:) forControlEvents:UIControlEventTouchDown];
            if (index == 0)
            {
                _selectedTabBarItem  =  tabbarButton;
                _selectedTabBarItem.selected = YES;
                [self tabBarItemDidSelected:_selectedTabBarItem];
            }
            [self addSubview:tabbarButton];
        }
    }
    return self;
}

-(void)setSelectedIndex:(NSInteger)selectedIndex
{
    _selectedIndex = selectedIndex;
    TabBarItemButton *selectedItem = (TabBarItemButton*)[self viewWithTag:_selectedIndex+1000];
    _selectedTabBarItem.selected = NO;
    selectedItem.selected = YES;
    _selectedTabBarItem  = selectedItem;
    if (self.delegate && [self.delegate respondsToSelector:@selector(tabBarViewItem:fromIndex:toIndex:)])
    {
        [self.delegate tabBarViewItem:self fromIndex:_selectedTabBarItem.tag toIndex:selectedItem.tag];
    }
}

-(void)tabBarItemDidSelected:(TabBarItemButton*)tabBarItem
{
    _selectedTabBarItem.selected = NO;
    tabBarItem.selected = YES;
    if (self.delegate && [self.delegate respondsToSelector:@selector(tabBarViewItem:fromIndex:toIndex:)])
    {
        [self.delegate tabBarViewItem:self fromIndex:_selectedTabBarItem.tag toIndex:tabBarItem.tag];
    }
    _selectedTabBarItem = tabBarItem;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
}

@end
